<?php
/**
 * Niushop商城系统 - 团队十年电商经验汇集巨献!
 * =========================================================
 * Copy right 2019-2029 山西牛酷信息科技有限公司, 保留所有权利。
 * ----------------------------------------------
 * 官方网址: https://www.niushop.com.cn
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用。
 * 任何企业和个人不允许对程序代码以任何形式任何目的再发布。
 * =========================================================
 */

namespace addon\supply\model;

use app\model\BaseModel;
use think\facade\Cache;

/**
 * 供应商表
 */
class Supplier extends BaseModel
{
	
	/**
	 * 添加供应商
	 */
	public function addSupplier($data)
	{
		$count = model('supplier')->getCount([['title', '=', $data['title']]]);
		if ($count > 0) {
			return $this->error('', '该供应商已经存在！');
		}
		$res = model('supplier')->add($data);
        Cache::tag("supply")->clear();
		return $this->success($res);
	}

    /**
     * 修改供应商
     */
	public function editSupplier($condition, $data) {
	    $res = model('supplier')->update($data, $condition);
        Cache::tag("supply")->clear();
	    return $this->success($res);
    }

    /**
     * 删除供货商
     * @param $condition
     */
    public function deleteSupplier($supplier_id) {
        $goods_count = model('goods')->getCount([['supplier_id', '=', $supplier_id]]);
        if ($goods_count > 0) {
            return $this->error('', '供应商下有商品，不可删除');
        }
        $res = model('supplier')->delete([['supplier_id', '=', $supplier_id]]);
        Cache::tag("supply")->clear();
        return $this->success($res);
    }

	/**
	 * 获取供应商分页列表
	 * @param array $condition
	 * @param number $page
	 * @param string $page_size
	 * @param string $order
	 * @param string $field
	 */
	public function getSupplierPageList($condition = [], $page = 1, $page_size = PAGE_LIST_ROWS, $order = '', $field = '*')
	{
        $data = json_encode([ $condition, $field, $order, $page, $page_size ]);
        $cache = Cache::get("supplier_getSupplierPageList_" . $data);
        if (!empty($cache)) {
            return $this->success($cache);
        }
		$list = model("supplier")->pageList($condition, $field, $order, $page, $page_size);
        Cache::tag("supply")->set("supplier_getSupplierPageList_" . $data, $list);
		return $this->success($list);
	}

	/**
	 * 获取供应商信息
	 * @param unknown $condition
	 * @param string $field
	 */
	public function getSupplierInfo($condition = [], $field = '*'){
        $data = json_encode([ $condition, $field ]);
        $cache = Cache::get("supplier_getSupplierInfo_" . $data);
        if (!empty($cache)) {
            return $this->success($cache);
        }
	    $info = model("supplier")->getInfo($condition, $field);
        Cache::tag("supply")->set("supplier_getSupplierInfo_" . $data, $info);
	    return $this->success($info);
	}
}